package com.tjf.myijkplayer;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import com.blankj.utilcode.util.LogUtils;
import com.tjf.myijkplayer.views.VideoPlayerIJK;
import com.tjf.myijkplayer.views.VideoPlayerListener;

import java.util.ArrayList;
import java.util.List;

import tv.danmaku.ijk.media.player.IMediaPlayer;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    List<String> videoList = new ArrayList<>();
    int videoIndex;
    VideoPlayerIJK mIjkvideo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mIjkvideo = findViewById(R.id.mijkvideo);
        setVideoUri();
        startPlayVideo();
    }


    public void setVideoUri() {
        videoList.clear();
        String videoPath = "http://v-cdn.zjol.com.cn/280443.mp4";
        String videoPath1 = "http://rbv01.ku6.com/omtSn0z_PTREtneb3GRtGg.mp4";
        videoList.add(videoPath);
        videoList.add(videoPath1);
    }

    public void startPlayVideo() {
        videoIndex = 0;
        mIjkvideo.setFollowTheVideoHeight(true);
        mIjkvideo.setVideoControlBar(true);
        mIjkvideo.setListener(new VideoPlayerListener() {

            @Override
            public void onBufferingUpdate(IMediaPlayer iMediaPlayer, int i) {

            }

            @Override
            public void onCompletion(IMediaPlayer iMediaPlayer) {
                LogUtils.iTag(TAG, "onCompletion");
                videoIndex++;
                if (videoIndex >= videoList.size()) {
                    videoIndex = 0;
                }
                mIjkvideo.setVideoPath(videoList.get(videoIndex));
            }

            @Override
            public boolean onError(IMediaPlayer iMediaPlayer, int i, int i1) {

                return false;
            }

            @Override
            public boolean onInfo(IMediaPlayer iMediaPlayer, int i, int i1) {
                return false;
            }

            @Override
            public void onPrepared(IMediaPlayer iMediaPlayer) {
                LogUtils.iTag("播放器-", iMediaPlayer.getVideoHeight());
                mIjkvideo.start();
                ;
            }

            @Override
            public void onSeekComplete(IMediaPlayer iMediaPlayer) {
                LogUtils.iTag(TAG, "onSeekComplete");
            }
        });
        mIjkvideo.setVideoPath(videoList.get(videoIndex));
    }

    @Override
    protected void onPause() {
        super.onPause();
        mIjkvideo.release();
    }
}